import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.beans.PropertyChangeSupport;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;

/**
 * JPanel that can be swapped out into the Display and used for adding items to
 * the Ship's ItemBag
 */
public class Shop extends State
{
    // public PropertyChangeSupport status;
    JPanel main;
    private int foodButtonSpaceV = 10; // pixels for vertical rigid area between
                                       // food buttons
    private int foodP, waterP, repairP, medicineP; // respective prices

    public ItemBag shopItems;

    /**
     * Create the panel.
     */
    public Shop(Ship s)
    {
        super();
        theShip = s;
        // status = new PropertyChangeSupport(this);
        
        shopItems = new ItemBag();
        foodP = 10;
        waterP = 5;
        repairP = 20;
        medicineP = 10;

        create();
    }

    /**
     * Creates the panel
     */
    public void create()
    {
        setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

        // setPreferredSize(new Dimension(800,700));

        main = new JPanel();
        main.setLayout(new BoxLayout(main, BoxLayout.X_AXIS));
        main.setAlignmentX(LEFT_ALIGNMENT);
        // main.setBackground(Color.blue);
        JPanel left = new JPanel(); // left is for buttons to buy
        left.setLayout(new BoxLayout(left, BoxLayout.Y_AXIS));
        left.setAlignmentX(LEFT_ALIGNMENT);
        left.setMaximumSize(new Dimension(500, 600));
        // left.setBackground(Color.RED);
        JPanel right = new JPanel(); // right displays shop amounts
        // right.setMaximumSize(new Dimension(300,600));

        left.setLayout(new BoxLayout(left, BoxLayout.Y_AXIS));
        left.setAlignmentX(LEFT_ALIGNMENT);

        right.setLayout(new BoxLayout(right, BoxLayout.PAGE_AXIS));

        main.add(left);
        main.add(right);

        // Store title
        JLabel lblStore = new JLabel("Store");
        lblStore.setFont(new Font("Rockwell", Font.BOLD, 18));
        add(lblStore);
        add(main);

        // Price Labels
        JLabel foodPrice = new JLabel("Food: $" + foodP + ", weight "
                + theShip.getInventory().foodW);
        JLabel waterPrice = new JLabel("Water: $" + waterP + ", weight "
                + theShip.getInventory().waterW);
        JLabel repairPrice = new JLabel("Repair Parts: $" + repairP
                + ", weight " + theShip.getInventory().repairW);
        JLabel medicinePrice = new JLabel("Medicine: $" + medicineP
                + ", weight " + theShip.getInventory().medicineW);

        // Buttons with pics of item, and actionListeners
        BufferedImage bread = new BufferedImage(50, 50, 1);
        try
        {
            bread = ImageIO.read(new File("bread.jpg"));
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        JButton picFood = new JButton(new ImageIcon("bread.jpg"));
        picFood.addActionListener(new foodListener());
        picFood.setBackground(Color.white);

        BufferedImage hammer = new BufferedImage(1, 1, 1);
        try
        {
            hammer = ImageIO.read(new File("hammer.jpg"));
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        JButton picRepair = new JButton(new ImageIcon(hammer));
        picRepair.addActionListener(new repairListener());
        picRepair.setBackground(Color.white);

        BufferedImage medicine = new BufferedImage(1, 1, 1);
        try
        {
            medicine = ImageIO.read(new File("medicine.jpg"));
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        JButton picMedicine = new JButton(new ImageIcon(medicine));
        picMedicine.addActionListener(new medicineListener());
        picMedicine.setBackground(Color.white);

        BufferedImage water = new BufferedImage(1, 1, 1);
        try
        {
            water = ImageIO.read(new File("water.jpg"));
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        JButton picWater = new JButton(new ImageIcon(water));
        picWater.addActionListener(new waterListener());
        picWater.setBackground(Color.white);

        // Finished Button
        JButton finished = new JButton("Finished");
        finished.setAlignmentY(BOTTOM_ALIGNMENT);
        finished.addActionListener(new finishedListener());

        // food buttons
        picFood.setAlignmentX(Component.CENTER_ALIGNMENT);
        picRepair.setAlignmentX(Component.CENTER_ALIGNMENT);
        picWater.setAlignmentX(Component.CENTER_ALIGNMENT);
        picMedicine.setAlignmentX(Component.CENTER_ALIGNMENT);
        foodPrice.setAlignmentX(Component.CENTER_ALIGNMENT);
        repairPrice.setAlignmentX(Component.CENTER_ALIGNMENT);
        waterPrice.setAlignmentX(Component.CENTER_ALIGNMENT);
        medicinePrice.setAlignmentX(Component.CENTER_ALIGNMENT);

        // add buttons and labels to left
        left.add(picFood);
        left.add(foodPrice);
        left.add(Box.createRigidArea(new Dimension(0, foodButtonSpaceV)));
        left.add(picWater);
        left.add(waterPrice);
        left.add(Box.createRigidArea(new Dimension(0, foodButtonSpaceV)));
        left.add(picRepair);
        left.add(repairPrice);
        left.add(Box.createRigidArea(new Dimension(0, foodButtonSpaceV)));
        left.add(picMedicine);
        left.add(medicinePrice);
        left.add(Box.createRigidArea(new Dimension(0, foodButtonSpaceV)));

        // right.add(new
        // JLabel("You have: $"+theShip.getInventory().getMoney()));
        right.add(finished);

    }

    /**
     * BUTTON LISTENERS each listener adds 1 to the ship's itembag and subtracts
     * money from the ship's itembag each time the button is clicked
     */
    private class foodListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            if (theShip.getInventory().enoughWeight("food"))
            {
                if (theShip.getInventory().consumeMoney(foodP)) theShip
                        .getInventory().addFood(1);
                else
                {
                    JOptionPane.showMessageDialog(main,
                            "Arrgggghhh matey, you are\nout of coin!",
                            "Plunder more!", JOptionPane.WARNING_MESSAGE);
                }
            }
            else
            {
                JOptionPane.showMessageDialog(main,
                        "Arrgggghhh matey, you've got\nno storage room left!",
                        "Get a bigger ship!", JOptionPane.WARNING_MESSAGE);
            }
            status.firePropertyChange("inventory", false, true);
        }
    }

    /**
     * Water Button Listener
     */
    private class waterListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            if (theShip.getInventory().enoughWeight("water"))
            {
                if (theShip.getInventory().consumeMoney(waterP)) theShip
                        .getInventory().addWater(1);
                else
                {
                    JOptionPane.showMessageDialog(main,
                            "Arrgggghhh matey, you are\nout of coin!",
                            "Plunder more!", JOptionPane.WARNING_MESSAGE);
                }
            }
            else
            {
                JOptionPane.showMessageDialog(main,
                        "Arrgggghhh matey, you've got\nno storage room left!",
                        "Get a bigger ship!", JOptionPane.WARNING_MESSAGE);
            }
            status.firePropertyChange("inventory", false, true);
        }
    }
    /**
     * Repair button listener
     */
    private class repairListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            if (theShip.getInventory().enoughWeight("repair"))
            {
                if (theShip.getInventory().consumeMoney(repairP)) theShip
                        .getInventory().addRepair(1);
                else
                {
                    JOptionPane.showMessageDialog(main,
                            "Arrgggghhh matey, you are\nout of coin!",
                            "Plunder more!", JOptionPane.WARNING_MESSAGE);
                }
            }
            else
            {
                JOptionPane.showMessageDialog(main,
                        "Arrgggghhh matey, you've got\nno storage room left!",
                        "Get a bigger ship!", JOptionPane.WARNING_MESSAGE);
            }
            status.firePropertyChange("inventory", false, true);
        }
    }
    
    /**
     * Medicine button listener
     */
    private class medicineListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            if (theShip.getInventory().enoughWeight("medicine"))
            {
                if (theShip.getInventory().consumeMoney(medicineP)) theShip
                        .getInventory().addMedicine(1);
                else
                {
                    JOptionPane.showMessageDialog(main,
                            "Arrgggghhh matey, you are\nout of coin!",
                            "Plunder more!", JOptionPane.WARNING_MESSAGE);
                }
            }
            else
            {
                JOptionPane.showMessageDialog(main,
                        "Arrgggghhh matey, you've got\nno storage room left!",
                        "Get a bigger ship!", JOptionPane.WARNING_MESSAGE);
            }
            status.firePropertyChange("inventory", false, true);
        }
    }

    /**
     * Finished button listener
     */
    private class finishedListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            status.firePropertyChange("finished", false, true);
        }
    }
    
    /**
     * type
     */
    public String getType(){
    	return "Shop";
    }
}